package com.qf.dao.impl;

import com.qf.dao.GoodsDao;
import com.qf.domain.Goods;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class GoodDaoImpl implements GoodsDao {
    private QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public long getCount(String where, List<Object> params) {
        String sql = " select count(*) from tb_goods "+where;
        try {
            return queryRunner.query(sql,new ScalarHandler<>(),params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Goods> selectByPage(int page_num, int page_size, String where, List<Object> params) {
        params.add((page_num-1)*page_size);
        params.add(page_size);
        String sql = "select *from tb_goods "+where+" order by id limit ?,?  ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Goods.class),params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Goods selectById(Integer gid) {
        String sql = " select * from tb_goods where id = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Goods.class),gid);//在JDBC编程中，Java的Integer对象会被自动转换为Java.sql包中的java.sql.Integer，进而能够与数据库中的int类型字段进行匹配。
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(Goods goods) {
        String sql = " INSERT INTO `tb_goods` (name,`pubdate`,`picture`,`price`,`star`,`intro`,`typeid`) VALUES  (?,?,?,?,?,?,?)";
        Object[] params = {goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTypeid()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public List<Goods> findAll()  {
        try {
            return queryRunner.query("select * from tb_goods",new BeanListHandler<>(Goods.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(String gid)  {
        try {
            queryRunner.update("delete from tb_goods where id=?",gid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Goods goods) {
        String sql ="update tb_goods set name=?, pubdate=?, picture=? ,price=?  ,star=?  ,intro=? ,typeid=? where id=?";
        Object []param={goods.getName(),goods.getPubdate(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTypeid(),goods.getId()};
        try {
            queryRunner.update(sql,param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Goods> selectByUAD(StringBuilder where1, List<Object> i)  {
        String sql="select * from tb_goods "+where1;
        try {
            List<Goods> goodsList = queryRunner.query(sql, new BeanListHandler<>(Goods.class),i.toArray());
            return goodsList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
